package com.chinasolf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chinasoft.model.hosp.Department;
import com.chinasoft.vo.hosp.DepartmentVo;
import com.chinasolf.repository.DepartmentRepository;
import com.chinasolf.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void saveDept(Map<String, Object> map) {
        //map转换为department对象
        String jsonString = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(jsonString, Department.class);

        //根据医院编号+可是编号查询是否存在科室数据
        Department departmentExist =
                            departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        //判断
        if(departmentExist != null) { //修改
            department.setId(departmentExist.getId());
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> selectPage(int page, int limit) {
        //设置排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        //设置分页
        Pageable pageable = PageRequest.of(page-1,limit,sort);

        //封装条件
        Department department = new Department();
        Example<Department> example = Example.of(department);

        //调用方法分页查询
        Page<Department> pageModel = departmentRepository
                .findAll(example, pageable);
        return pageModel;
    }

    @Override
    public void removeDept(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            departmentRepository.deleteById(department.getId());
        }

    }
    //根据医院编号查询医院里面的所有科室
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //1.根据hoscode把医院中的所有科室查询出来
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);

        //2.把查询出来的所有科室list集合转换要求格式
        //分组操作 stream
        //map集合,key:分组大科室编号 value:大科室所有记录集合(有多少个小科室集合)
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //创建最终封装集合
        List<DepartmentVo> result = new ArrayList<>();
        //遍历分组之后得到map集合,进行封装
        for(Map.Entry<String,List<Department>> entry: departmentMap.entrySet()){
            //获取map里面key值,大科室编号
            String bigcode = entry.getKey();
            //获取map里面的value值,大科室分组所有数据,一个大科室所有小科室集合
            List<Department> smallDeptList = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            //大科室编号
            departmentVo.setDepcode(bigcode);
            //大科室名称
            //大科室里面可能只有一个小科室,从第一个值获取大科室名称
            String bigname = smallDeptList.get(0).getBigname();
            departmentVo.setDepname(bigname);
            //封装小科室
            //创建一个list集合封装大科室里面所有小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department departmentSmall: smallDeptList){
                DepartmentVo departmentVoSmall = new DepartmentVo();
                departmentVoSmall.setDepcode(departmentSmall.getDepcode());
                departmentVoSmall.setDepname(departmentSmall.getDepname());
                //放到list
                children.add(departmentVoSmall);

            }
            //把每次封装大科室对象放到最终list集合
            departmentVo.setChildren(children);
            result.add(departmentVo);
        }
        //返回
        return result;
    }

    @Override
    public Department getDeptName(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode,depcode);
    }
}
